package cn.cfoodc.sharding.controller;

import cn.cfoodc.sharding.domain.TComplex;
import cn.cfoodc.sharding.domain.TOrderItem;
import cn.cfoodc.sharding.service.TComplexService;
import org.apache.shardingsphere.sharding.algorithm.keygen.SnowflakeKeyGenerateAlgorithm;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;
import java.util.UUID;
import java.util.zip.DataFormatException;

/**
 * 按照createDatetime日期分表
 */
@RestController
public class TComplexController {

    private TComplexService tComplexService;
    private SnowflakeKeyGenerateAlgorithm NEXT_ID ;
    @Autowired
    public TComplexController(TComplexService tComplexService,SnowflakeKeyGenerateAlgorithm snowflakeKeyGenerateAlgorithm){
        this.tComplexService=tComplexService;
        this.NEXT_ID=snowflakeKeyGenerateAlgorithm;
    }


    @PostMapping("/addTComplex")
    @Transactional(rollbackFor = { Exception.class })
    @ShardingTransactionType(TransactionType.XA)// 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
    public int insert(@RequestParam("createTime") Timestamp createTime ){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TComplex t = new TComplex(
                (Long)NEXT_ID.generateKey(),(Long)NEXT_ID.generateKey(), UUID.randomUUID().toString(),UUID.randomUUID().toString(),
                Timestamp.valueOf(sdf.format(createTime)),
                Timestamp.valueOf(sdf.format(new Timestamp(System.currentTimeMillis()).getTime())),
                Timestamp.valueOf(sdf.format(new Timestamp(System.currentTimeMillis()).getTime()))
        );
        return  tComplexService.insertOne(t);
    }

    //按照user_id定位到库，在当前库执行多表查询
    @GetMapping("/getOneComplex")
    public TComplex getOne(@RequestParam("userId") Long userId , @RequestParam("id") Long id){
        TComplex complex = tComplexService.selectByPrimaryKey(userId , id);
        return complex ;
    }


    //按照user_id定位到库，按照createDatetime定位到命中一个表，
    @GetMapping("/getOneComplex2")
    public TComplex getOne(@RequestParam("userId") Long userId , @RequestParam("createDatetime") Timestamp createDatetime){
        TComplex complex = tComplexService.selectByUseridAndCreatedate(userId , createDatetime);
        return complex ;
    }

    //按照创建时间全库全表查询，
    @GetMapping("/getAllTComplex")
    public List<TComplex> selectAll( @RequestParam("benginCreateDatetime") Timestamp benginCreateDatetime , @RequestParam("endCreateDatetime") Timestamp endCreateDatetime){
        return tComplexService.selectAll(benginCreateDatetime , endCreateDatetime);
    }


}
